import Decimal from 'decimal.js'
// 二次封装uni.showToast()
export const Toast = (title: string) => {
  return new Promise(reslove => {
    uni.showToast({
      title,
      icon: 'none',
    });
    let time = setTimeout(() => {
      clearTimeout(time);
      reslove(time);
    }, 3000);
  });
};

// 对数组下标0和1换位置
export const changeIndex0_1 = (list: any[]) => {
  if (!list.length) return [];
  // 使用临时变量进行交换
  let temp = list[0];
  list[0] = list[1];
  list[1] = temp;
  return list;
};

export function formatSecondsToMMSS(seconds: number) {
  const min = Math.floor(seconds / 60);
  const sec = Math.floor(seconds % 60);
  return `${min}:${sec < 10 ? '0' + sec : sec}`;
}
// 使用decimal来处理计算
// 加 plus 减 minus 乘 times 除 div
// 分转元
export function formatUnitCentToYuan(cent: number, fixed=0) {
  // 异常值处理
  if (cent === null || cent === undefined || isNaN(cent) || !isFinite(cent)) {
    return '0';
  }
  if (typeof cent !== 'number') {
    const parsed = Number(cent);
    if (isNaN(parsed) || !isFinite(parsed)) return '0';
    cent = parsed;
  }
  if (cent < 0) {
    return '0';
  }
  try {
    const num = new Decimal(cent); // 分子
    const divNum = new Decimal(100); // 分母
    const result = num.div(divNum).toFixed(fixed)
    return result
  } catch (e) {
    return '0';
  }
}

// 元转分
export function formatUnitYuanToCent(yuan: number, fixed=0) {
  yuan = Number(yuan);
  if (
    yuan === null ||
    yuan === undefined ||
    isNaN(yuan) ||
    !isFinite(yuan) ||
    yuan < 0
  ) {
    return Number(0).toFixed(fixed);
  }
  try {
    const num = new Decimal(yuan);
    const timesNum = new Decimal(100);
    const result = num.times(timesNum).toFixed(fixed)
    return result
  } catch (e) {
    return Number(0).toFixed(fixed);
  }
}
// 订单状态枚举
export function orderStatus(status:number) {
  let statusName = ''
  switch (status) {
    case 0:
      statusName = '待支付'
      break;
    case 10:
      statusName = '待审核'
      break;
    case 20:
      statusName = '待打款'
      break;
    case 30:
      statusName = '已完成'
      break;
    case 40:
      statusName = '已取消'
      break;
    default:
      break;
  }
  return statusName
}

// 我的商品状态枚举
export function goodsStatus(status:number) {
  let statusName = ''
  switch (status) {
    case 0:
      statusName = '已下架'
      break;
    case 1:
      statusName = '出售中'
      break;
    case 2:
      statusName = '已出售'
      break;
    default:
      break;
  }
  return statusName
}

// 二级分类枚举值
export function subType(type: number) {
  let name = ''
  switch (type) {
    case 1:
      name = '诗词'
      break;
    case 2:
      name = '成曲'
      break;
    case 3:
      name = 'demo'
      break;
    default:
      break;
  }
  return name
}

// 登录状态管理
let isNavigatingToLogin = false; // 防止重复跳转登录页

export const checkLoginAndNavigate = () => {
  const auth = uni.getStorageSync('auth');
  if (!auth && !isNavigatingToLogin) {
    isNavigatingToLogin = true;
    uni.navigateTo({ 
      url: '/pages/sub/login/index',
      success: () => {
        // 跳转成功后重置标志
        setTimeout(() => {
          isNavigatingToLogin = false;
        }, 1000);
      },
      fail: () => {
        // 跳转失败也重置标志
        isNavigatingToLogin = false;
      }
    });
    return false;
  }
  return !!auth;
};
